package com.zksk.admin.controller.productEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksk.admin.domain.productRelease.dto.ProductReleaseDto;
import com.zksk.admin.domain.productRelease.dto.ProductReleaseLimitDto;
import com.zksk.admin.domain.productRelease.vo.ProductReleaseVo;
import com.zksk.admin.service.productEnum.ProductReleaseService;
import com.zksk.common.core.domain.R;
import com.zksk.common.core.utils.StringUtils;
import com.zksk.common.core.web.controller.BaseController;
import com.zksk.common.log.annotation.Log;
import com.zksk.common.log.enums.BusinessType;
import com.zksk.mbg.admin.domain.*;
import com.zksk.mbg.admin.mapper.ProductReleaseMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@SuppressWarnings("all")
@RestController
@RequestMapping("/productRelease")
@Api(value = "ProductReleaseController", tags = "产品关联客户集合")
public class ProductReleaseController extends BaseController {

    @Autowired
    private ProductReleaseService productReleaseService;
    @Autowired
    private ProductReleaseMapper productReleaseMapper;

    /**
     * 分页查询产品列表
     * @param:  * @param page
     * @param pageSize
     * @param platformProductEnum
     * @return: R<Page>
     * @author: hyz
     * @date: 2022/12/2
     */
    @PostMapping("/proReleaseList")
    @ApiOperation(value = "获取产品关联客户集合列表")
    public R<Page> list(@RequestBody ProductReleaseLimitDto productReleaseLimitDto){
        log.info("page:{} pageSize:{}",productReleaseLimitDto.getPageNum(),productReleaseLimitDto.getPageSize());
        //构建分页
        Page<ProductRelease> pageInfo = new Page<>(productReleaseLimitDto.getPageNum(),productReleaseLimitDto.getPageSize());

        //构建条件
        LambdaQueryWrapper<ProductRelease> wrapper = new LambdaQueryWrapper<ProductRelease>()
                .like(StringUtils.isNotBlank(productReleaseLimitDto.getName()), ProductRelease::getProductEnumName, productReleaseLimitDto.getName())
                .like(StringUtils.isNotBlank(productReleaseLimitDto.getCustomerName()),ProductRelease::getCustomerName,productReleaseLimitDto.getCustomerName())
                .eq(StringUtils.isNotBlank(productReleaseLimitDto.getOnlineStatus()), ProductRelease::getOnlineStatus, productReleaseLimitDto.getOnlineStatus())
                .orderByAsc(ProductRelease::getCreateTime);

        Page<ProductRelease> page = productReleaseService.page(pageInfo, wrapper);
        return R.ok(page);
    }

    @GetMapping("/proReleaseDetails")
    @ResponseBody
    @ApiOperation(value = "查看产品关联客户详情")
    public R<ProductReleaseVo> checkDetails(Long id){
        ProductReleaseVo proVo = new ProductReleaseVo();
        ProductRelease proRelease =  productReleaseMapper.selectById(id);
        Long productEnumId = proRelease.getProductEnumId();
        Long orgId  = proRelease.getOrgId();
        ProductRelease productRelease  = productReleaseService.checkProductReleaseDetails(id);
        proVo.setProductRelease(productRelease);
        List<Rate> customerList = productReleaseService.getCustomerRate(id);
        proVo.setCustomerRateList(customerList);
        List<Rate> orgList = productReleaseService.selectOrgRate(productEnumId,orgId);
        proVo.setOrgRateList(orgList);
        return R.ok(proVo);
    }

    /**
     * 方法描述
     * @param:  * @param productEnumId
     * @param orgId
     * @return: List<Rate>
     * @author: hyz
     * @date: 2022/12/29
     */
    @GetMapping("/getOrgRate")
    @ApiOperation(value = "查询机构费率列表")
    public List<Rate> getOrgRate(Long productEnumId,Long orgId){
        if(productEnumId ==null){
            log.info("productEnumId 和 orgId 不能为空，请检查！");
            return null;
        }
        List<Rate> rate = productReleaseService.selectOrgRate(productEnumId,orgId);
        return rate;

    }

    @GetMapping("/getCustomerNmaeList")
    @ApiOperation(value = "查询客户名称列表")
    public List<Customer> getCustomerNmaeList(){
       List<Customer> list =  productReleaseService.selectCustomerNameList();
       return list;
    }

    @GetMapping("/getOrgNameList")
    @ApiOperation(value = "查询机构名称列表")
    public List<Org> getOrgNameList(Long productEnumId){
        List<Org> list = productReleaseService.selectOrgNameList(productEnumId);
        return  list;
    }

    /**
     * 添加关联产品
     * @param:  * @param dto
     * @return: R<String>
     * @author: hyz
     * @date: 2022/12/12
     */
    @PostMapping("/addProRelease")
    @ApiOperation(value = "添加产品关联客户集合列表")
    public R<ProductRelease> addProduct(@RequestBody ProductReleaseDto dto){
        ProductRelease proRelease = productReleaseService.setProductReleaseValue(dto);
        return R.ok(proRelease,"保存成功");
    }

    @PostMapping("/updatePro")
    @ApiOperation(value = "修改产品关联客户集合列表")
    public R<String> updatePro(@RequestBody ProductReleaseDto dto){
        ProductRelease productRelease = productReleaseService.getById(dto.getId());
        if (dto.getId() == null || dto.getId() <=0 || productRelease == null){
            return R.fail("更新失败");
        }
        ProductRelease proRelease = productReleaseService.setProductReleaseValue(dto);
        if(proRelease !=null){
            return R.ok("修改成功");
        }else{
            return R.fail("修改失败");
        }
    }

    @ResponseBody
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除产品")
    public R<String> deleteProduct(@PathVariable("ids") List<Long> ids){
        if(ids.size()<0) {
            return null;
        }
        boolean bool = productReleaseService.removeBatchByIds(ids);
        if(bool){
            return R.ok("删除成功");
        }else{
            return R.fail("删除失败");
        }
    }


    /**
     *  通过客户id获取 对应的列表数据
     * @param customerId
     * @return
     */
    @Log(title = "产品管理", businessType = BusinessType.SELECT)
    @ApiOperation(value = "通过客户id获取 对应的列表数据")
    @GetMapping("/getListByCustomerId")
    public R<List<ProductRelease>> getListByCustomerId(@RequestParam("customerId") Long customerId){

        List<ProductRelease> list = productReleaseService.list(new QueryWrapper<ProductRelease>()
                .eq(ProductRelease.CUSTOMER_ID, customerId));
        return R.ok(list);
    }
}
